package org.ww.order.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ww.common.constant.RabbitMqConstant;
import org.ww.common.to.mq.SeckillOrderTo;
import org.ww.order.service.OrderService;

import java.io.IOException;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-10-31 16:11
 **/
@Slf4j
@Service
@RabbitListener(queues = RabbitMqConstant.ORDER_SECKILL_ORDER_QUEUE)
public class SeckillOrderListener {

    @Autowired
    private OrderService orderService;
    @RabbitHandler
    public void handlerSeckillOrderMessage(Message message, Channel channel, SeckillOrderTo orderTo) throws IOException {
        log.info("收到订单秒杀信息：【{}】",orderTo);
        try{
            orderService.createSeckillOrder(orderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
